import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/dialog/custom_general_dialog.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_high_input_page.dart';
import 'package:enterprise_v6_app/app/core/values/gaps.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/couser_center/get_detail_by_id_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/demand_research/questionnaire_detail/questionnaire_detail_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/education/quiz/model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

const _blue = Color(0xFF5C6DFF);

///调查问卷
class QuestionnaireDetailPage extends StatelessWidget {
  QuestionnaireDetailPage({super.key});

  final controller = Get.put(QuestionnaireDetailController(172800));

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        // ignore: deprecated_member_use
        return WillPopScope(
          child: Scaffold(
            appBar: CustomAppBar(
              title: Text(
                '调查问卷',
                style: TextStyle(
                  fontSize: 16.w,
                  color: const Color(0xFF262F38),
                ),
              ),
            ),
            backgroundColor: const Color(0xFFF5F5F6),
            body: _buildBody(),
            bottomSheet: SafeArea(child: _buildBottom()),
          ),
          onWillPop: () async {
            return await Get.dialog<bool>(
                  const CustomGeneralDialog(
                    content: '确认退出吗？',
                  ),
                ) ??
                false;
          },
        );
      },
    );
  }

  /// 主要
  Widget _buildBody() {
    return ListView(
      children: [
        AppGaps.vGap16,
        _buildTop(),
        AppGaps.vGap16,
        _buildQuiz(),
        AppGaps.vGap50,
      ],
    );
  }

  /// 题目顶部 倒计时 / 字体大小切换
  Widget _buildTop() {
    return Padding(
      padding: EdgeInsets.symmetric(
        horizontal: 18.w,
      ),
      child: Container(
        width: 343.w,
        height: 48.w,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.w),
          color: Colors.white,
        ),
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _buildTopLeft(),
            _buildTextSizeSwitch(),
          ],
        ),
      ),
    );
  }

  /// 倒计时 / 题目对错指示器
  Widget _buildTopLeft() {
    final leftTime = controller.leftTime;
    return Row(
      children: [
        Stack(
          alignment: Alignment.bottomCenter,
          children: [
            Container(
              width: 48.w,
              height: 5.w,
              decoration: BoxDecoration(
                  color: Colors.blue,
                  borderRadius: BorderRadius.circular(4.w),
                  gradient: LinearGradient(
                    colors: [
                      Colors.blue.shade100,
                      Colors.blue.shade500,
                      Colors.blue,
                    ],
                  )),
            ),
            Text(
              '倒计时',
              style: TextStyle(
                fontSize: 16.w,
                color: const Color(0xFF333333),
              ),
            )
          ],
        ),
        AppGaps.hGap6,
        _buildTopTimeSingle(leftTime.hours),
        _buildTopTimeDivider(),
        _buildTopTimeSingle(leftTime.minutes),
        _buildTopTimeDivider(),
        _buildTopTimeSingle(leftTime.seconds)
      ],
    );
  }

  /// 倒计时 单个数字
  Widget _buildTopTimeSingle(int time) {
    return Container(
      width: 20.w,
      height: 20.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: _blue, borderRadius: BorderRadius.circular(4.w)),
      child: Text(
        time.toString().padLeft(2, '0'),
        style: TextStyle(fontSize: 14.w, color: Colors.white),
      ),
    );
  }

  /// 倒计时 分割线
  Widget _buildTopTimeDivider() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 2.w),
      child: Text(
        ':',
        style: TextStyle(fontSize: 18.w, color: Colors.blue),
      ),
    );
  }

  /// 字体大小切换
  Widget _buildTextSizeSwitch() {
    final child = Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: QuizTextSize.values.map(_buildTextSizeSwitchSingle).toList(),
    );
    return Container(
      padding: EdgeInsets.all(1.w),
      decoration: BoxDecoration(border: Border.all(color: _blue, width: 1.w), borderRadius: BorderRadius.circular(4.w)),
      child: child,
    );
  }

  /// 字体大小切换 单个按钮
  Widget _buildTextSizeSwitchSingle(QuizTextSize size) {
    final current = controller.textSize.value == size;
    return InkWell(
      onTap: () {
        if (!current) {
          controller.textSize.value = size;
          controller.update();
        }
      },
      child: Container(
        width: 28.w,
        height: 28.w,
        decoration: BoxDecoration(
          color: current ? _blue : Colors.white,
          borderRadius: BorderRadius.circular(4.w),
        ),
        alignment: Alignment.center,
        child: Text(
          'Aa',
          style: TextStyle(
            fontSize: (size.toInt - 6).w,
            color: current ? Colors.white : _blue,
          ),
        ),
      ),
    );
  }

  /// 题目 主要
  Widget _buildQuiz() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Stack(
        children: [
          Container(
            height: 20.w,
            decoration: BoxDecoration(
              color: _blue,
              borderRadius: BorderRadius.circular(10.w),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(top: 2),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10.w),
            ),
            child: _buildQuizContent(),
          ),
        ],
      ),
    );
  }

  /// 题目 内容
  Widget _buildQuizContent() {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '请选择需要培训的学习专题',
            style: TextStyle(
              color: const Color(0xFF262F38),
              fontWeight: FontWeight.w500,
              fontSize: 16.sp,
            ),
          ),
          Column(
            children: controller.jypxztQ1uSZQaD.map(_questW).toList(),
          ),
          CustomHighInputPage(
            title: '我的建议：',
            text: controller.meContent,
            hintText: '请输入建议',
            onCallBack: (value) {
              controller.meContent = value;
            },
            isEnabled: true,
            isRequired: false,
            isShowDivider: false,
          ),
        ],
      ),
    );
  }

  /// 底部的按钮
  Widget _buildBottom() {
    return Container(
      width: 375.w,
      height: 50.w,
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: const Color(0x0D000000),
            offset: Offset(0, -3.w),
            blurRadius: 2.w,
            spreadRadius: 0,
          ),
        ],
      ),
      child: Padding(
        padding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 16.w),
        child: InkWell(
          onTap: controller.onClickToPage,
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 9.w),
            decoration: BoxDecoration(
              color: const Color(0xFF5C6DFF),
              borderRadius: BorderRadius.circular(44.w),
            ),
            alignment: Alignment.center,
            child: Text(
              '提交',
              style: TextStyle(
                fontSize: 16.w,
                color: Colors.white,
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _questW(GetDetailByIdSurveyTypes item) {
    return Container(
      margin: EdgeInsets.only(top: 10.w),
      child: Row(
        children: [
          _colorfulCheckBox(item),
          AppGaps.hGap10,
          Row(
            children: [
              Text(
                item.surveyTypeDicName ?? '',
                style: TextStyle(
                  fontSize: controller.textSize.value.toInt.w,
                  color: Colors.black,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _colorfulCheckBox(GetDetailByIdSurveyTypes item) {
    return Theme(
      data: ThemeData(unselectedWidgetColor: const Color(0xFFDCDCDC)),
      child: SizedBox(
        width: 16.w,
        height: 16.w,
        child: Checkbox(
            overlayColor: MaterialStateProperty.all(Colors.blue),
            materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
            value: item.checkboxSelected,
            onChanged: (value) {
              item.checkboxSelected = !item.checkboxSelected;
              controller.update();
            }),
      ),
    );
  }
}
